package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.Common.Common;
import com.itany.pojo.Book;
import com.itany.pojo.Examine;
import com.itany.service.ExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Author：侍家宇
 * Date：2019-09-18 下午2:26
 * Description：<描述>
 */
@Controller
@RequestMapping("examine")
public class ExamineController {
    @Autowired
    private ExamineService examineService;

    /**
     * 入库审核添加（供应商）
     *
     * @param book
     * @param file
     * @param session
     * @return
     */
    @RequestMapping("/addexamine")
    public ModelAndView addExamine(Book book, @RequestParam("file") CommonsMultipartFile file, HttpSession session) {
        ModelAndView mv = new ModelAndView();
        System.out.println("..................................");


        try {
         String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String newName=uuid+ filename.substring(filename.lastIndexOf("."));
        String pathName="/home/soft01/upload/"+ newName;
        file.transferTo(new File(pathName));
        book.setImgurl(pathName);
           examineService.addbook(book);
           examineService.addBookSupplier(book);

            Examine examine = new Examine();
            examine.setTitle(book.getBookname());
            examine.setNumber(book.getNumber());
            examine.setBookid(book.getId());
            examine.setFlag(Common.ExamineCommons.EXAMINE_ENABLE);
            examine.setSupplierid(1);
            examineService.addExamine(examine);

        mv.setViewName("supplier_examine");
        } catch (IOException e) {
            mv.addObject("msg","文件上传失败");
            mv.setViewName("supplier_examine");

        }




        return mv;
    }


    /**
     * 查询审核
     */

   @ResponseBody
    @RequestMapping("/findAll")
    public Map<String, Object> findAll(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer rows) {

        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<Examine> info = examineService.findExamineAll(page, rows);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    /**
     * 查看详情
     * @param id
     * @return
     */
    @RequestMapping("findAllBook")
    @ResponseBody
    public Book findBook(int id){
        Book book=examineService.findBook(id);
       return book;
    }

    /**
     * 入库审核供应商
     * 查询书籍
     *
     */
    @RequestMapping("/findByCondition")
    @ResponseBody
    public Map<String, Object> findByCondition(Book book,@RequestParam(defaultValue = "1")Integer page,
                                               @RequestParam(defaultValue = "10")Integer rows) {
        System.out.println(book.getBookname()+"-----------------------");
        System.out.println(book.getISBN()+"==========================");
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<Examine> info = examineService.findByCondition(book,page,rows);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    /**
     * 平台审核
     */
    @RequestMapping("/modifyExamine")
    public String modifyExamine(Examine examine){
        examineService.modifyExamine(examine);
        return "redirect:/manager/showexamine";
    }
}
